import http from '@/common/api/http.js'
import appStore from '@/common/app-store.js'

export default {
	isInitFinish: false,
	// 0=H5,1=安卓,2=iOS,3=微信小程序
	systemType: 3,
	//设备号
	deviceId: '',
	//版本号
	version: '',
	//内部版本号
	baseVersion: '1.0.00',
	//api环境,1=正式线,2=预发布,3=测试线
	apiSetting: 1,
	isDebug: true, //控制网络请求日志
	apiDomain: '',
	appName: '玩安卓-UniApp',
	// 安卓本地打包app报名 : com.seven.wanandroid
	androidLocalPackageName: '',
	miniAppInfo: {
		miniAppName: '',
		miniAppLogo: '',
		miniAppVersion: '1.0.00'
	},
	//uni-app个推手机唯一标识
	clientId: '',
	// 初始化,在request里面执行
	init() {
		if (this.isInitFinish) return;
		this.isInitFinish = true;
		// #ifdef MP-WEIXIN
		let miniExt = uni.getExtConfigSync();
		if (miniExt) {
			this.miniAppInfo = miniExt
		} else {
			this.miniAppInfo = {
				miniAppName: 'WanAndroid小程序',
				miniAppLogo: 'http://wx.qlogo.cn/mmopen/gvY7ibH7Tw7CC2tbklO7wccNib3dDhA6GXjtSYmmno5rwoTKM4aV6cqyOmDcPUI0jj9pibsfNkgyQqPQGecY4ic61zApFwkuHzBh/0',
				miniAppVersion: this.baseVersion
			}
		}
		// #endif
		this.systemType = this.initSystemPlatform();
		this.deviceId = this.initDeviceId();
		this.version = this.initVersion();
		this.apiSetting = this.initApiSetting();
		this.apiDomain = this.initApiDomain();
		this.appName = this.initAppName();
		this.isDebug = this.initDebugStatus();
		// #ifdef APP-PLUS
		this.initPushClientId();
		if (!this.clientId) {
			this.getClientInfo();
		}
		// #endif

		console.log('app-config :: ');
		console.log('systemType = ' + this.systemType +
			' | deviceId = ' + this.deviceId +
			' | isDebug = ' + this.isDebug +
			' | version = ' + this.version +
			' | baseVersion = ' + this.baseVersion +
			' | apiSetting = ' + this.apiSetting +
			' | apiDomain = ' + this.apiDomain +
			' | appName = ' + this.appName +
			' | clientId = ' + this.clientId);
	},
	//安卓手机app环境获取应用包名
	getAndroidPackageName() {
		if (this.androidLocalPackageName != '') {
			return androidLocalPackageName;
		} else {
			let context = plus.android.runtimeMainActivity();
			let packageName = context.getPackageName();
			return packageName;
		}
	},
	//获取当前应用环境参数
	initSystemPlatform() {
		// #ifdef APP-PLUS
		if (uni.getSystemInfoSync().platform == "android") {
			return 1;
		} else if (uni.getSystemInfoSync().platform == "ios") {
			return 2;
		}
		// #endif

		// #ifdef H5
		return 0
		// #endif

		// #ifdef MP-WEIXIN
		return 3
		// #endif
	},
	initDeviceId() {
		// #ifdef APP-PLUS
		return plus.device.uuid;
		// #endif

		// #ifdef H5
		return 'H5';
		// #endif

		// #ifdef MP-WEIXIN
		return 'miniapp';
		// #endif
	},
	//获取应用版本号
	initVersion() {
		// #ifdef APP-PLUS
		return plus.runtime.version;
		// #endif

		// #ifdef MP-WEIXIN
		return this.miniAppInfo.miniAppVersion;
		// #endif

		return 0;
	},
	//获取api环境
	initApiSetting() {
		// #ifdef APP-PLUS
		if (uni.getSystemInfoSync().platform == "android") {
			try {
				let metaClassName = this.getAndroidPackageName() + '.utils.TslmMetaDataUtils';
				let metaDataUtils = plus.android.importClass(metaClassName);
				return metaDataUtils.getApiSetting();
			} catch (e) {
				return this.apiSetting;
			}
		} else if (uni.getSystemInfoSync().platform == "ios") {
			try {
				let metaDataUtils = plus.ios.importClass("TslmMetaDataUtils");
				return metaDataUtils.getApiSetting();
			} catch (e) {
				return this.apiSetting;
			}
		}
		// #endif

		// #ifdef MP-WEIXIN || H5
		//微信环境在打包的时候动态修改apiSetting
		return this.apiSetting;
		// #endif
	},
	// 获取api域名
	initApiDomain() {
		return 'https://www.wanandroid.com';
	},
	initAppName() {

		// #ifdef APP-PLUS
		if (uni.getSystemInfoSync().platform == "android") {
			try {
				let metaClassName = this.getAndroidPackageName() + '.utils.TslmMetaDataUtils';
				let metaDataUtils = plus.android.importClass(metaClassName);
				return metaDataUtils.getAppName();
			} catch (e) {
				return this.appName;
			}
		} else if (uni.getSystemInfoSync().platform == "ios") {
			try {
				let metaDataUtils = plus.ios.importClass("TslmMetaDataUtils");
				return metaDataUtils.getAppName();
			} catch (e) {
				return this.appName;
			}
		}
		// #endif

		// #ifdef H5
		return this.appName;
		// #endif

		// #ifdef MP-WEIXIN
		return this.miniAppInfo && this.miniAppInfo.miniAppName ? this.miniAppInfo.miniAppName : this.appName;
		// #endif
	},
	initDebugStatus() {
		// #ifdef APP-PLUS
		if (uni.getSystemInfoSync().platform == "android") {
			try {
				let metaClassName = this.getAndroidPackageName() + '.utils.TslmMetaDataUtils';
				let metaDataUtils = plus.android.importClass(metaClassName);
				return metaDataUtils.isDebug();
			} catch (e) {
				return this.isDebug;
			}
		} else if (uni.getSystemInfoSync().platform == "ios") {
			try {
				let metaDataUtils = plus.ios.importClass("TslmMetaDataUtils");
				return metaDataUtils.isDebug();
			} catch (e) {
				return this.isDebug;
			}
		}
		// #endif

		// #ifdef MP-WEIXIN || H5
		//微信环境在打包的时候动态修改apiSetting
		return this.isDebug;
		// #endif
	},
	initPushClientId() {
		let clientId = appStore.getStorageByKey(appStore.pushClientId)
		if (clientId) {
			this.clientId = clientId;
		}
	},
	setClientId(clientId = '') {
		this.clientId = clientId;
		appStore.setStorageByKey(appStore.pushClientId, clientId)
	},
	getClientInfo() {
		//获取推送通道
		uni.getProvider({
			service: 'push',
			success: e => {
				let providerList = e.provider;
				//开启push接收
				uni.subscribePush({
					provider: providerList[0],
					success: res => {
						plus.push.getClientInfoAsync(info => {
							if (info.clientid) {
								this.setClientId(info.clientid);
							}
						}, e => {})
					},
					fail: err => {
						this.setClientId();
					}
				});
			},
			fail: e => {
				this.setClientId();
			}
		});
	}
}
